<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Jcc;
use app\common\model\room\Lists;
use fast\Http;
use think\Db;
use think\Queue;

class Accept extends Api
{
    protected $noNeedLogin = "*";
    protected $noNeedRight = "*";

    //接收三方群内通知推送
    public function room_info()
    {
        $data = $this->request->param();
//        if (isset($data['msglist'][0]['fromtype']) && $data['msglist'][0]['fromtype'] == 2) {
        if (isset($data['msglist'][0]['fromtype'])) {
            Db::name('third_info')->insert([
                'info' => json_encode($data),
                'createtime' => date("Y-m-d H:i:s")
            ]);
            if ($data['msglist'][0]['msgtype'] == 49) {
                $teamId = $data['msglist'][0]['teamId'] ?? 0;
                $list = Lists::where(["team_id" => $teamId])->find();
                if (!$list) {
                    //teamId 房间ID  fromid 个人ID  fromgid
                    try {
                        Queue::push("app\common\job\Room@query", [
                            "uin" => $data['msglist'][0]['uin'] ?? 0,
                            "teamId" => $teamId,
                            "fromid" => $data['msglist'][0]['fromid'] ?? 0,
                            "fromgid" => $data['msglist'][0]['fromgid'] ?? 0,
                            "fromname" => $data['msglist'][0]['toname'] ?? 0
                        ], "query");
                    } catch (\Exception $exception) {
                        $this->success("n", $exception->getMessage());
                    }

                }
            }
        }
        $this->success("w");
    }

    public function test_info()
    {
        $data = $this->request->param();
        Db::name('third_test')->insert([
            'info' => json_encode($data),
            'createtime' => date("Y-m-d H:i:s")
        ]);
        $this->success();
    }

    //获取session_id
    public function session_id()
    {
        $this->success("", Jcc::session());
    }

    //查询房间信息
    public function query_room()
    {
        $third_url = Db::name('config')->where(['name' => 'third_url'])->value("value", "");
        $teamId = $this->request->param("teamId");
        $json = json_encode(["teamId" => $teamId, "session_id" => Jcc::session()]);
        $result = Http::post("{$third_url}}GetTeamData", $json);
        echo $teamId;
        var_dump(json_decode($result, true));
    }

    //给群聊发送消息
    public function send_msg()
    {
//        $result = Http::post("http://8.217.109.142:30001/SendTextMsg", '{"wxid": "34452088728@chatroom","msg": "测试1"}');
        $wxid = $this->request->param("wxid");
        $msg = $this->request->param("msg");
        $result = Jcc::send_msg($wxid, $msg, '57295543-1697192415');
        var_dump($result);
//        $this->success("", json_decode($result));
    }
}